package com.spark.shiro.interceptor;

import com.spark.shiro.bean.PageBean;
import com.sun.rowset.internal.Row;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

/**
 * Created by tyd on 2017-10-31.
 */

@Intercepts({
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
        @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class})
})
public class PageInterceptor2 implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        if (invocation.getTarget() instanceof Executor) {

            Object[] args = invocation.getArgs();

            MappedStatement mappedStatement = (MappedStatement) args[0];
            String sqlId = mappedStatement.getId();

            if (sqlId.endsWith("ByPage")) {

                Executor executor = (Executor) invocation.getTarget();
                Object parameter = args[1];

                Map map = (HashMap) parameter;
                PageBean pageBean = (PageBean) map.get("pageBean");

                RowBounds rowBounds = (RowBounds) args[2];

                ResultHandler resultHandler = (ResultHandler) args[3];

                List list = executor.query(mappedStatement, parameter, rowBounds, resultHandler);

                pageBean.setTotal(list.size());

                BoundSql boundSql = mappedStatement.getBoundSql(parameter);

                String sql = boundSql.getSql();

                StringBuilder sqlBuilder = new StringBuilder(sql);
                sqlBuilder.append(" limit ");
                sqlBuilder.append(pageBean.getIndex());
                sqlBuilder.append(",");
                sqlBuilder.append(pageBean.getPageNum());

                BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), sqlBuilder.toString(), boundSql.getParameterMappings(), parameter);

                CacheKey cacheKey = null;
                if (args.length == 4) {
                    cacheKey = executor.createCacheKey(mappedStatement, parameter, rowBounds, newBoundSql);
                }
                List resultList = executor.query(mappedStatement, parameter, rowBounds, resultHandler, cacheKey, newBoundSql);

                return resultList;
            }


        }
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {

    }
}
